package com.fhnew.app.ui.dialogactivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.ResmsReg;
import com.fhnew.app.model.network.entity.req.Resmsg;
import com.fhnew.app.model.network.entity.req.changeNikeNameBean;
import com.fhnew.app.util.CheckView;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class PhoneDiolog extends Activity {

    ImageView close;
    TextView titletext;
    TextView phonenuber;
    TextView smsendbt;
    EditText etusername;
    EditText code_imget;
    EditText etsms;
    LinearLayout editlayout;
    LinearLayout tuxinglayout;
    LinearLayout smslayout;
    CheckView codeImg;
    TextView nextbt;
    TextView nextbt1;
    TextView nextbt2;
    TextView layout3;
    LinearLayout commitlayout;
    Boolean SMSFLAG;
    String[] code_reslut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Constants.backgroundvoic = false;
        setContentView(R.layout.activity_phone_diolog);
        close = findViewById(R.id.close);
        phonenuber = findViewById(R.id.phonenuber);
        titletext = findViewById(R.id.titletext);
        etusername = findViewById(R.id.etusername);
        editlayout = findViewById(R.id.editlayout);
        nextbt = findViewById(R.id.nextbt);
        commitlayout = findViewById(R.id.commitlayout);
        nextbt1 = findViewById(R.id.nextbt1);
        codeImg = findViewById(R.id.code_img);
        layout3 = findViewById(R.id.layout3);
        nextbt2 = findViewById(R.id.nextbt2);
        tuxinglayout = findViewById(R.id.tuxinglayout);
        smslayout = findViewById(R.id.smslayout);
        code_imget = findViewById(R.id.code_imget);
        etsms = findViewById(R.id.etsms);
        smsendbt = findViewById(R.id.smsendbt);
        getSMSreg();
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        smsendbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HttpJsonUtils.getHttpApiInstance()
                        .sendmsg(Constants.getuseAdress() + "api/m/smsCode/" +
                                etusername.getText().toString().trim(), "1", "")
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new CustomObserver<Resmsg>() {
                            @Override
                            protected void onCustomNext(Resmsg resLogin) {
                                if (resLogin.status) {
                                    //成功
                                    timer.start();
                                    Toast.makeText(PhoneDiolog.this, resLogin.msg, Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(PhoneDiolog.this, resLogin.msg, Toast.LENGTH_SHORT).show();
                                    timer.cancel();
                                }
                            }

                            @Override
                            protected void onSubscribeCustom(Disposable disposable) {
                            }

                            @Override
                            protected void onErrorCustom(Throwable e) {

                            }
                        });
            }
        });
        nextbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(etusername.getText().toString().trim())) {
                    editlayout.setVisibility(View.GONE);
                    nextbt.setVisibility(View.GONE);
                    nextbt1.setVisibility(View.VISIBLE);
                    commitlayout.setVisibility(View.VISIBLE);
                    phonenuber.setText(etusername.getText().toString().trim());
                } else {
                    Toast.makeText(PhoneDiolog.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                }
            }
        });
        nextbt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        code_reslut = codeImg.getValidataAndSetImage();//生成验证码
        codeImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                code_reslut = codeImg.getValidataAndSetImage();//生成验证码
            }
        });
        nextbt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SMSFLAG) {
                    if (TextUtils.isEmpty(etsms.getText().toString())) {
                        commitphonenumber();
                    } else {
                        Toast.makeText(PhoneDiolog.this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    //图形验证
                    //开始检验本地验证码
                    String yanzheng = code_reslut[0] + code_reslut[1] + code_reslut[2] + code_reslut[3];
                    if (code_imget.getText().toString().equals(yanzheng)) {
                        //提交手机号
                        commitphonenumber();
                    } else {
                        Toast.makeText(PhoneDiolog.this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    private void commitphonenumber() {
        //开始网络请求
        HttpJsonUtils.getHttpApiInstance()
                .change_phone(Constants.getuseAdress() + "api/m/bindPhone",
                        phonenuber.getText().toString().trim(), etsms.getText().toString().trim(), UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<changeNikeNameBean>() {
                    @Override
                    protected void onCustomNext(changeNikeNameBean resBanner) {
                        if (resBanner.getCode() == 0) {
                            Toast.makeText(PhoneDiolog.this, "绑定手机号成功", Toast.LENGTH_LONG).show();
                            UserModel.user.isPhone = "1";
                            nextbt1.setVisibility(View.GONE);
                            commitlayout.setVisibility(View.GONE);
                            layout3.setVisibility(View.VISIBLE);
                            nextbt2.setVisibility(View.VISIBLE);
                        } else {
                            Toast.makeText(PhoneDiolog.this, "" + resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                    }
                });
    }

    private void getSMSreg() {
        HttpJsonUtils.getHttpApiInstance()
                .smsReg(Constants.getuseAdress() + "api/m/smsReg")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResmsReg>() {
                    @Override
                    protected void onCustomNext(ResmsReg resmsReg) {
                        SMSFLAG = resmsReg.status;
                        if (resmsReg.status) {
                            //手机验证
                            smslayout.setVisibility(View.VISIBLE);
                            tuxinglayout.setVisibility(View.GONE);
                        } else {
                            //图形验证
                            tuxinglayout.setVisibility(View.VISIBLE);
                            smslayout.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                    }
                });
    }

    CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            smsendbt.setText((millisUntilFinished / 1000) + "S后重发");
            smsendbt.setEnabled(false);
        }

        @Override
        public void onFinish() {
            smsendbt.setEnabled(true);
            smsendbt.setText("获取验证码");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
//    @OnClick({R.id.close, R.id.nextbt})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.close:
//                finish();
//                break;
//            case R.id.nextbt:
//                break;
//        }
//    }
}
